//
//  BGView.m
//  多点触摸
//
//  Created by 伍银荣 on 2021/7/6.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "BGView.h"

@interface BGView ()

@property (nonatomic, strong) NSArray *imagesArr;

@end

@implementation BGView

//懒加载
-(NSArray *)imagesArr {
    if (!_imagesArr) {
        _imagesArr = @[[UIImage imageNamed:@"red"],[UIImage imageNamed:@"green"]];
    }
    return _imagesArr;
}


//但手指开始触摸view的时候调用
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"touchesBegan");
    [self addImgView:touches];
    [self setNeedsDisplay];
}

//手指在view上移动的时候调用
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"touchesMoved");
    [self addImgView:touches];
    //需要重绘
    [self setNeedsDisplay];
}


//默认touches是不支持多点触控的,需要勾选上storyBoard中这个view的multiple-Touch选项
- (void)addImgView:(NSSet *)touches {
    int i = 0;
    for (UITouch *touch in touches) {
        //获取手指的位置
        CGPoint currentPoint = [touch locationInView:self];
        //创建imageView对象
        UIImageView *imgView = [[UIImageView alloc]initWithImage:self.imagesArr[i]];
        //设置imageView的center为当前点击的位置
        imgView.center = currentPoint;
        [self addSubview:imgView];
        //开启动画,点击2秒后自动消失
        [UIView animateWithDuration:2 animations:^{
            //将图片view的透明度设置为0
            imgView.alpha = 0;
        } completion:^(BOOL finished) {
            if (finished) {
                //完成后将imgView从当前view上移除
                [imgView removeFromSuperview];
            }
        }];
        i++;
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
